﻿using Flame.Reflection;
using Flame.Socket;
using System;
using System.Collections.Generic;
using System.Text;

namespace Flame.Dlr
{
    class AutoITExec : IExecutable, IDisposable
    {
        AutoItSocket _socket;
        string _exePath; 
        string _hostName; 
        int _port;

        List<string> _script = new List<string>();

        public AutoITExec(string exePath, string hostName = "127.0.0.1", int port = 8000)
        {
            _exePath = exePath;
            _hostName = hostName;
            _port = port;
        }


        public void ClearScope(ScopeParameters scopeParameters)
        {
            if(_socket != null ) _socket.Close();
            _socket = new AutoItSocket(_exePath, _hostName, _port);
        }

        public Result Execute(string text)
        {
            _script.Add(text);
            StringBuilder sb = new StringBuilder();
            foreach (var s in _script)
                sb.Append(s + Environment.NewLine);
            return new Result() { Data = _socket.Execute(text) };
        }

        public ResultAssembly AddAssembly(AssemblyWrapper res)
        {
            return new ResultAssembly() { Loaded = false, Exception = null, Language = GetType() };
        }

        public void AddVariable(Variable variable)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            if (_socket != null) _socket.Close();
        }
        public IIntellisense Intellisense { get { return null; } }
    }
}
